package com.java.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.java.dao.MasterDao;
import com.java.util.Md5Util;
import com.java.vo.Master;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	MasterDao masterDao;
	
    @ResponseBody
    @RequestMapping(value = "/loginJson", method = RequestMethod.POST) 
    public void  loginJson(@RequestParam String Jdata,HttpServletRequest request, HttpServletResponse response) {
    	JSONObject json = JSON.parseObject(Jdata);
    	JSONObject jsonData = JSON.parseObject(json.get("data").toString());
    	String username = jsonData.getString("username");
    	String password = jsonData.getString("password");
    	
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET, POST");
		Map<Object, Object> map = new HashMap<Object, Object>();
		try {
			if(ObjectUtils.equals(null, username)){
				map.put("returnCode", false);
				map.put("returnMSG", "username null!");
				response.getWriter().println(JSON.toJSONString(map));
				response.flushBuffer();
				return;
			}
			if(ObjectUtils.equals(null, password)){
				map.put("returnCode", false);
				map.put("returnMSG", "password null!");
				response.getWriter().println(JSON.toJSONString(map));
				response.flushBuffer();
				return;
			}
			int flag = masterDao.getUserPassword(username, password);
			if(flag<=0){
				map.put("returnCode", false);
				map.put("returnMSG", "login error!");
				response.getWriter().println(JSON.toJSONString(map));
				response.flushBuffer();
				return;
			}
			Master master = masterDao.getUserByName(username);
			map.put("returnCode", true);
			map.put("returnMSG", "login success!");
//			map.put("master", master);
			String token = Md5Util.GetMD5Code(master.getMasterId()+"zglc"+master.getName()+"cgbs")+UUID.randomUUID().toString();
//			loginStateService.add(token);
			map.put("token", token);
			map.put("userName", master.getName());
			map.put("userType", master.getMasterId());
			map.put("userId", master.getId());
//			request.getSession().removeAttribute("master");
//			request.getSession().setAttribute("master", master);
//			request.getSession().getAttribute("master");
			
			System.out.println("login:master:"+master.toString());
			response.getWriter().println(JSON.toJSONString(map));
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return;
    }
}
